package com.zhr.tea.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.entity.Pingfen;
import com.zhr.tea.entity.Pingjia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhang on 2016/11/4.
 */

public class PingjiaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<Pingjia.ContentBean> pingjiaData;
    private List<Pingfen.ContentBean> pingfenData;
    private Context context;

    List<String> data = new ArrayList<>();


    public static final int HEAD_TYPE = 1;
    public static final int ITEM_TYPE = 2;


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEAD_TYPE;
        } else {
            return ITEM_TYPE;
        }

    }

    public PingjiaAdapter(List<Pingjia.ContentBean> pingjiaData, List<Pingfen.ContentBean> pingfenData, Context context) {
        this.pingjiaData = pingjiaData;
        this.pingfenData = pingfenData;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh = null;
        View view = null;
        switch (viewType) {
            case HEAD_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_pingfen, null);
                vh = new HeadViewHolder(view);
                break;
            case ITEM_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_pingjia, null);
                vh = new ItemViewHolder(view);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {
            if (pingfenData.size() != 0) {
                String[] arg = pingfenData.get(0).getAllScoreCount().split("\\|");
                for (int i = 0; i < arg.length; i++) {
                    data.add(arg[arg.length - i - 1]);
                }
                ((HeadViewHolder) holder).pingfenTitle.setText("商品评分:" + pingfenData.get(0).getScoreLevel() + "  共"
                        + pingfenData.get(0).getScoreCount() + "份评论");

                for (int i = 0; i < ((HeadViewHolder) holder).pbList.size(); i++) {
                    ((HeadViewHolder) holder).pbList.get(i).setMax(pingfenData.get(0).getScoreCount());
                    ((HeadViewHolder) holder).pbList.get(i).setProgress(Integer.valueOf(data.get(i)));
                }
            }
        } else if (holder instanceof ItemViewHolder) {
            if (pingjiaData.size() != 0) {
                Glide.with(context).load(pingjiaData.get(position).getCheadImgUrl()).into(((ItemViewHolder) holder).pingjiaUserIm);
                ((ItemViewHolder) holder).pingjiaTime.setText(pingjiaData.get(position).getCreateDate());
                ((ItemViewHolder) holder).pingjiaUserTv.setText(pingjiaData.get(position).getContent());
                ((ItemViewHolder) holder).pingjiaRatbar.setRating(pingjiaData.get(position).getScoreLevel());
                if (pingjiaData.get(position).getGoodsImgs() != null) {
                    ((ItemViewHolder) holder).pingjiaContentIm.setVisibility(View.VISIBLE);
                    Glide.with(context).load(pingjiaData.get(position).getGoodsImgs()).into(((ItemViewHolder) holder).pingjiaContentIm);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return pingjiaData.size() + 1;
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.pingfen_title)
        TextView pingfenTitle;

        @BindView(R.id.pingfen_pb1)
        ProgressBar pingfenPb1;

        @BindView(R.id.pingfen_pb2)
        ProgressBar pingfenPb2;

        @BindView(R.id.pingfen_pb3)
        ProgressBar pingfenPb3;

        @BindView(R.id.pingfen_pb4)
        ProgressBar pingfenPb4;

        @BindView(R.id.pingfen_pb5)
        ProgressBar pingfenPb5;

        List<ProgressBar> pbList = new ArrayList<>();


        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            pbList.add(pingfenPb1);
            pbList.add(pingfenPb2);
            pbList.add(pingfenPb3);
            pbList.add(pingfenPb4);
            pbList.add(pingfenPb5);


        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.pingjia_user_im)
        ImageView pingjiaUserIm;
        @BindView(R.id.pingjia_time)
        TextView pingjiaTime;
        @BindView(R.id.pingjia_ratbar)
        RatingBar pingjiaRatbar;
        @BindView(R.id.pingjia_user_tv)
        TextView pingjiaUserTv;
        @BindView(R.id.pingjia_content_im)
        ImageView pingjiaContentIm;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}

